package com.yunhe.device.repository.device;

import com.yunhe.device.domain.device.Device;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName EquipmentRepository
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/16 15:44
 * @Version 1.0
 **/
@Repository
public interface EquipmentRepository extends Neo4jRepository<Device,Long> {

    @Query("match(e:Device)-[:HAS_EQUIPTYPEATTR{value:{value},equipTypeId:{id}}]->(ea:EquipmentAttributes{name:{name}})return e")
    Device findByEquipNameAndEquipTypeId(@Param("value") String value,@Param("id") Long equipTypeId,@Param("name") String equipAttrName);

    @Query("match(e:Device),(et:DeviceType) where ID(et)={equipTypeId} and ID(e)={id} create (e)-[:HAS_EQUIPTYPE]->(et)")
    void bindEquipType(@Param("equipTypeId") Long equipTypeId,@Param("id") Long id);

    @Query("match(e:Device),(ea:EquipmentAttributes) where ID(ea)={equipAttrId} and ID(e)={id} create (e)-[:HAS_EQUIPTYPEATTR{value:{value},equipTypeId:{equipTypeId}}]->(ea)")
    void bindEquipTypeAttr(@Param("equipTypeId") Long equipTypeId,@Param("id")  Long id,@Param("value")  String value,@Param("equipAttrId")  Long equipAttrId);

    @Query("match(o:Office)-[:HAS_EQUIP]->(e:Device) where ID(e)={id} return o.officeTitle")
    String findOfficeNameByEquipId(@Param("id") Long id);

    @Query("match(e:Device)-[heta:HAS_EQUIPTYPEATTR]->(ea:EquipmentAttributes) where ID(e)={id} delete heta")
    void unbindEquipAttr(@Param("id") Long id);

    @Query("match(e:Device)-[heta:HAS_EQUIPTYPEANLOGTYPE]->(at:PointDataType) where ID(e)={id} delete heta")
    void unbindAnlogType(@Param("id") Long id);

    @Query("match(e:Device),(at:PointDataType) where ID(at)={anlogTypeId} and ID (e)={equipId} create (e)-[:HAS_EQUIPTYPEANLOGTYPE{value:{value},equipTypeId:{equipTypeId}}]->(at)")
    void bindEquipAnlogType(@Param("equipTypeId") Long equipTypeId,@Param("equipId") Long equipId,@Param("value") String value,@Param("anlogTypeId") Long anlogTypeId);

    @Query("match(e:Device)-[het:HAS_EQUIPTYPE]->(et:DeviceType) where  ID (e)={id} delete het")
    void unbindEquipType(@Param("id") Long id);

    @Query("match(e:Device),(ea:EquipmentAttributes) where ID(ea)={equipAttrId} and ID(e)={id} create (e)-[:HAS_CUSTOMEQUIPTYPEATTR{value:{value}}]->(ea)")
    void bindCustomizeEquipAttr(@Param("id")  Long id,@Param("value")  String value,@Param("equipAttrId")  Long equipAttrId);

    @Query("match(e:Device)-[heta:HAS_CUSTOMEQUIPTYPEATTR]->(ea:EquipmentAttributes) where ID(e)={id} delete heta,ea")
    void unbindCustomEquipAttr(@Param("id")Long id);

    @Query("match(o:Office),(e:Device) where ID(e)={id} and ID(o)={officeId} create (o)-[:HAS_EQUIP]->(e)")
    void bindOfficeAndDevice(@Param("id") Long id,@Param("officeId")  Long officeId);

    @Query("match(o:Office)-[he:HAS_EQUIP]->(e:Device) where ID(e)={id} delete he")
    void unbindOffice(@Param("id") Long id);

    @Query("match(o:Office)-[he:HAS_EQUIP]->(e:Device) where ID(o)={officeId} return e")
    List<Device> findEquipByOfficeId(@Param("officeId") Long officeId);

    Device findByEquipName(String equipName);

    Device findByEquipNameAndOwnedPark(String equipName, Long ownedPark);
}
